<?php
/**
 * Created by PhpStorm.
 * User: myhonour
 * Date: 2017/10/7
 * Time: 19:42
 */

namespace app\admin\model;
use Auth\Auth;

class AuthHandle
{
    /** 白名单
     * @var array
     */
    private $whiteList  =   [
        'admin/index/index',
        'admin/index/main',
        'admin/weixin/check'
    ];

    protected $auth =   NULL; // OBJECT OR NULL

    /** 权限检测
     * @return bool
     */
    public function AuthCheck($name =   NULL){
        // 验证权限
        $rule_name   =   !is_null($name)?$name:\request()->module().'/'.\request()->controller().'/'.\request()->action();
        // 检查白名单 不需要验证的
        if (in_array(strtolower($rule_name),$this->whiteList)) return true;
        // 超级 管理员用户 不需要验证
        $uid    =   session(Admin::USER_KEY_STR.'.admin_id');
        if(config('superSysAdmin') == $uid) return true;
        // 验证
        $this->auth =   is_null($this->auth)?(new Auth()):$this->auth;
        if (!$this->auth->check($rule_name,$uid)){
            return false;
        }
        return true;
    }
}